Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasm-builder): add support for new wasm32v1-none target #7008

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

StackOverflowExcept1on
Copy link

@StackOverflowExcept1on StackOverflowExcept1on commented Dec 27, 2024

Description

Resolves #5777

Previously wasm-builder used hacks such as -Zbuild-std (required rust-src component) and RUSTC_BOOTSTRAP=1 to build WASM runtime without WASM features: sign-ext, multivalue and reference-types, but since Rust 1.84 (will be stable on 9 January, 2025) the situation has improved as there is new wasm32v1-none target that disables all "post-MVP" WASM features except mutable-globals.

Previously, your rust-toolchain.toml looked like this:

[toolchain]
channel = "stable"
components = ["rust-src"]
targets = ["wasm32-unknown-unknown"]
profile = "default"

It should now be updated to something like this:

[toolchain]
channel = "stable"
targets = ["wasm32v1-none"]
profile = "default"

To build the runtime:

cargo build --package minimal-template-runtime --release

Integration

If you are using Rust 1.84 and above, then install the wasm32v1-none target instead of wasm32-unknown-unknown as shown above. You can also remove the unnecessary rust-src component.

Also note the slight differences in conditional compilation:

  • wasm32-unknown-unknown: #[cfg(all(target_family = "wasm", target_os = "unknown"))]
  • wasm32v1-none: #[cfg(all(target_family = "wasm", target_os = "none"))]

Avoid using target_os = "unknown" in #[cfg(...)] or #[cfg_attr(...)] and instead use target_family = "wasm" or target_arch = "wasm32" in the runtime code.

Review Notes

Wasm builder requires the following prerequisites for building the WASM binary:

  • Rust >= 1.68 and Rust < 1.84:
    • wasm32-unknown-unknown target
    • rust-src component
  • Rust >= 1.84:
    • wasm32v1-none target
    • no more -Zbuild-std and RUSTC_BOOTSTRAP=1 hacks and rust-src component requirements!

@bkchr bkchr added the T17-primitives Changes to primitives that are not covered by any other label. label Dec 27, 2024
@github-actions github-actions bot requested a review from bkchr December 27, 2024 19:24
Copy link
Contributor

Review required! Latest push from author must always be reviewed

prdoc/pr_7008.prdoc Show resolved Hide resolved
@bkchr bkchr enabled auto-merge January 2, 2025 09:55
@StackOverflowExcept1on
Copy link
Author

How do I pass CI checks like SemVer and other?

@jasl
Copy link
Contributor

jasl commented Jan 8, 2025

Ping @bkchr It seems the CI won't trigger.

@jasl
Copy link
Contributor

jasl commented Jan 8, 2025

@StackOverflowExcept1on
Copy link
Author

StackOverflowExcept1on commented Jan 8, 2025

@jasl @bkchr I'll fix that in the next 30 minutes. Also wondering if we'll have to update the runners tomorrow, since the new version 1.84 will be stable tomorrow. I mean that the wasm32-unknown-unknown target should be replaced by wasm32v1-none on runners. We should come to something one on CI. Perhaps we should also replace all occurrences of “wasm32-unknown-unknown”.

auto-merge was automatically disabled January 8, 2025 17:19

Head branch was pushed to by a user without write access

@github-actions github-actions bot requested review from bkchr and koute January 8, 2025 17:19
@jasl
Copy link
Contributor

jasl commented Jan 8, 2025

@jasl @bkchr I'll fix that in the next 30 minutes. Also wondering if we'll have to update the runners tomorrow, since the new version 1.84 will be stable tomorrow. I mean that the wasm32-unknown-unknown target should be replaced by wasm32v1-none on runners. We should come to something one on CI. Perhaps we should also replace all occurrences of “wasm32-unknown-unknown”.

I'm not Parity staff, so I don't know...
I remember there was a critical accident in history by updating the Rust version, so I propose we not update CI.
Let Parity guys update their facilities.

@bkchr
Copy link
Member

bkchr commented Jan 8, 2025

Actually I realized something, @StackOverflowExcept1on can we change the pr to make the new target optional? So, it should still accept the wasm32-unknown-unknown target, but print some warning.

@StackOverflowExcept1on
Copy link
Author

@bkchr For now, do you want to use an environment variable like FORCE_NEW_WASM_TARGET to switch to wasm32v1-none and remove the warning?

@jasl
Copy link
Contributor

jasl commented Jan 8, 2025

Several required CI checks fail. Maybe the latest patch has trouble

@StackOverflowExcept1on
Copy link
Author

@jasl I think it's some kind of problem with std hitting wasmv1-none (it doesn't support it). It may have happened after recent merge of master.

@jasl
Copy link
Contributor

jasl commented Jan 9, 2025

I believe your original one is good, but I don't know why CI give you the false suggest.

@StackOverflowExcept1on Could you try don't change node = [...] and runtime-full = [...] just change the location of [dependencies.substrate-wasm-builder] ?

@jasl
Copy link
Contributor

jasl commented Jan 9, 2025

@bkchr Could you help to re-trigger CI?

@StackOverflowExcept1on I think you still need to apply @bkchr request

Just check for the available targets (I think we already have code for that) and then use the new target if available. If the new target is not available, print a warning to the build output.

Thank you for the work. I'm trying to migrate to Rust 2024 Edition when it's available.

@StackOverflowExcept1on
Copy link
Author

StackOverflowExcept1on commented Jan 9, 2025

@jasl I'm still working on this request. It would be nice to have the ability to run CI without pinging the parity team...

No. Just check for the available targets (I think we already have code for that) and then use the new target if available. If the new target is not available, print a warning to the build output.

@jasl
Copy link
Contributor

jasl commented Jan 9, 2025

@jasl I'm still working on this request. It would be nice to have the ability to run CI without pinging the parity team...

No. Just check for the available targets (I think we already have code for that) and then use the new target if available. If the new target is not available, print a warning to the build output.

This is a pain point. I guess it is about the cost.
When I fix CI fails, I usually use my local machine to replay failed CI checks, which would help to reduce attempts on GitHub Actions.

If possible, I would propose giving "verified" individual contributors an ability to trigger CI.

@bkchr
Copy link
Member

bkchr commented Jan 9, 2025

These are some annoying security requirements. Which someone apparently turned on again..

@jasl
Copy link
Contributor

jasl commented Jan 9, 2025

@bkchr
So https://github.com/paritytech/polkadot-sdk/actions/runs/12687523704/job/35380981212?pr=7008 still gives falsy suggestions...
Do you know who can help this?

@StackOverflowExcept1on
Copy link
Author

I should probably update prdoc as well: https://github.com/paritytech/polkadot-sdk/actions/runs/12687523719/job/35380940517
The only thing I don't know how to fix:

substrate-wasm-builder (substrate/utils/wasm-builder):
    Change stated in PR Doc: Minor
    Predicted semver change: Patch

@github-actions github-actions bot requested a review from bkchr January 10, 2025 17:14
@github-actions github-actions bot requested a review from bkchr January 13, 2025 00:58
@StackOverflowExcept1on
Copy link
Author

@bkchr I think basically did what you wanted without caching. The only problem I have encountered is the need to do a cargo clean.

$ cargo +stable build --package minimal-template-runtime --release
   Compiling minimal-template-runtime v0.0.0 (/home/user/polkadot-sdk/templates/minimal/runtime)
warning: [email protected]: You are building WASM runtime using `wasm32-unknown-unknown` target, although Rust >= 1.84 supports `wasm32v1-none` target!
warning: [email protected]: You can install it with `rustup target add wasm32v1-none --toolchain stable-x86_64-unknown-linux-gnu` if you're using `rustup`.
warning: [email protected]: After installing `wasm32v1-none` target, you must rebuild WASM runtime from scratch, use `cargo clean` before building.
    Finished `release` profile [optimized] target(s) in 3m 28s

@StackOverflowExcept1on
Copy link
Author

@bkchr @koute friendly ping

substrate/utils/wasm-builder/src/lib.rs Outdated Show resolved Hide resolved
/// Returns whether the `wasm32v1-none` target is installed in this version of the toolchain.
fn is_wasm32v1_none_target_installed(&self) -> bool {
let dummy_crate = DummyCrate::new(self, RuntimeTarget::Wasm, true);
let toolchain = dummy_crate.get_toolchain().expect("toolchain not found");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If get_toolchain returns an error, we should return false.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can we please just move get_toolchain() to cargo command?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, we can't just move get_toolchain() to the cargo command because cargo rustc, which is used by get_toolchain() under the hood, requires --manifest-path for dummy crate:

$ cargo rustc --manifest-path=./app/Cargo.toml -q -- --print sysroot
/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu

$ cargo rustc -q -- --print sysroot 
error: could not find `Cargo.toml` in `/tmp` or any parent directory

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If get_toolchain returns an error, we should return false.

ok, I did

let Some(toolchain) = dummy_crate.get_toolchain() else {
  return false;
};

[workspace]
"#,
);
match target {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't compile fn main() {} for wasm32v1-none because it doesn't have stdlib

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so i just decided to compile no_std cdylib with empty panic handler (runtime does the same thing, see wasm_project.rs file)

@github-actions github-actions bot requested a review from bkchr January 18, 2025 04:13
@StackOverflowExcept1on
Copy link
Author

@bkchr @koute I've resolved all the conversations. Could you please take a look and run CI?

}

fn is_target_installed(toolchain: &str, target: &str) -> bool {
Command::new("rustup")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://users.rust-lang.org/t/determining-installed-target-list-when-rustup-is-not-in-use/100594/2

Please use this combined with DummyCrate::().syroot and then check if the folder exists. This way it doesn't require rustup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T17-primitives Changes to primitives that are not covered by any other label.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider removing useless -sign-ext flag in substrate-wasm-builder
4 participants